<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar left-arrow class="page-nav-bar" title="登录">
      <van-icon name="cross" slot="left" @click="$router.back()" />
    </van-nav-bar>
    <!-- 登录表单 -->
    <van-form @submit="onSubmit" ref="userFormRef">
      <van-field v-model="user.mobile" name="mobile" placeholder="请输入手机号" :rules="userFormRules.mobile">
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field v-model="user.code" name="code" placeholder="请输入验证码" :rules="userFormRules.code">
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <!-- 倒计时 -->
        <van-count-down :time="1000 * 60" format="ss s" slot="button" v-if="isCountDownShow" @finish="isCountDownShow = false" />
        <van-button native-type="button" size="small" type="primary" slot="button" class="yzm-btn" @click="onSendSms" v-else>发送验证码</van-button>
      </van-field>
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit" class="login-btn">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendSms } from '@/api/user'
export default {
  name: 'LoginIndex',
  data () {
    return {
      user: {
        mobile: '',
        code: ''
      },
      userFormRules: {
        mobile: [
          { required: true, message: '请填写手机号！' },
          { pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/, message: '手机号码格式有误' }
        ],
        code: [
          { required: true, message: '请输入验证码！' },
          { pattern: /^\d{6}$/, message: '验证码格式有误' }
        ]
      },
      isCountDownShow: false
    }
  },
  methods: {
    // 登录表单验证事件
    async onSubmit () {
      this.$toast.loading({
        message: '登录中...',
        forbidClick: true,
        duration: 0
      })
      try {
        const { data } = await login(this.user)
        // console.log(data)
        this.$store.commit('setUser', data.data)
        this.$toast.success('登录成功')
        this.$router.back()
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.fail('手机号或验证码有误')
        } else {
          this.$toast.fail('登录失败！，请稍后再试。')
        }
      }
    },
    // 发送验证码
    async onSendSms () {
      // 校验手机号格式
      try {
        await this.$refs.userFormRef.validate('mobile')
      } catch (err) {
        return this.$toast.fail('手机号格式有误')
      }
      this.isCountDownShow = true
      // 3. 请求发送验证码
      try {
        await sendSms(this.user.mobile)
        this.$toast('发送成功')
      } catch (err) {
        // 发送失败，关闭倒计时
        this.isCountDownShow = false
        if (err.response.status === 429) {
          this.$toast('发送太频繁了，请稍后重试')
        } else {
          this.$toast('发送失败，请稍后重试')
        }
      }
    }
  }
}
</script>

<style scoped lang="less">
.toutiao {
  font-size: 37px;
}
.login-btn {
  border-radius: 10px;
  background-color: #6db4fb;
  .van-button__text {
    font-size: 30px;
  }
}
.yzm-btn {
  width: 152px;
  height: 46px;
  background-color: #ededed;
  border-radius: 23px;
  color: #666666;
  border: none;
  font-size: 22px;
  position: relative;
  &::after {
    content: '';
    display: block;
    position: absolute;
    width: 2px;
    height: 46px;
    left: -20px;
    top: 0;
    background-color: #ccc;
    z-index: 100;
  }
}
.page-nav-bar .van-icon {
  color: #fff;
}
</style>
